﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ExameRegister.Assert;
using System.Collections.ObjectModel;
using ExameRegister.Model;

namespace ExameRegister
{
    public partial class MainControl : UserControl
    {
        ServiceUtility su = new ServiceUtility();
        public MainControl()
        {
            InitializeComponent();
            su.OnCompletedCallService += new EventHandler<WCFEvent>(su_OnCompletedCallService);
            su.CallService("ExameRegister.BO.LoginBO", "GetPublicOfficialInfo", new object[] { "Status=1" }, this, "GetPublicOfficialInfo");
            //hlbExist.AddHandler(HyperlinkButton.MouseLeftButtonDownEvent, (seder, e)+>{
            //}, true);
            //var page = new MainPage(this);
            //page.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(page_PropertyChanged);
            //LayoutRoot.Children.Add(page);
        }

        void page_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //LayoutRoot.Children.Clear();
            if (LayoutRoot.Children.Contains(sender as UIElement))
                LayoutRoot.Children.Remove(sender as UIElement);
            hlbExist.Visibility = System.Windows.Visibility.Visible;
            var UI = new FlipSwitch(hlbExist) { Margin = new Thickness(0, 0, 0, 0) };
            Grid.SetColumn(UI, 1);
            Grid.SetRow(UI, 1);
            LayoutRoot.Children.Add(UI);
        }

        void su_OnCompletedCallService(object sender, WCFEvent e)
        {
            string method = e.UserState as string;
            switch(method){
                case "GetPublicOfficialInfo":
                    if (LayoutRoot.Children.Count >= 2)
                        LayoutRoot.Children.RemoveAt(1);
                    ObservableCollection<PublicOfficialInfo> infos = Utility.ConvertFromJson<ObservableCollection<PublicOfficialInfo>>(e.Result);
                    var entity = infos.FirstOrDefault();
                    if (entity != null)
                    {
                        var page = new MainPage(this);
                        Grid.SetColumnSpan(page, 3);
                        Grid.SetRowSpan(page, 2);
                        //Grid.SetColumnSpan(page, 3);
                        page.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(page_PropertyChanged);
                        LayoutRoot.Children.Add(page);
                    }
                    else
                    {
                       // LayoutRoot.Children.Clear();
                        var ui = new ErrorControl(hlbExist);
                        Grid.SetColumnSpan(ui, 2);
                        Grid.SetColumn(ui, 1);
                        Grid.SetRow(ui, 1);
                       LayoutRoot.Children.Add(ui);
                    }
                    break;
            }
        }

      
        void OKButton_Click(object sender, RoutedEventArgs e)
        {
            su.CallService("ExameRegister.BO.LoginBO", "GetPublicOfficialInfo", new object[] { "Status=1" }, this, "GetPublicOfficialInfo");
        }

        private void hlbExist_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确定退出报名？", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
            cm.OKButton.Click -= new RoutedEventHandler(OKButton_Click);
            cm.OKButton.Click += new RoutedEventHandler(OKButton_Click);
            cm.Show();
        }

       

       
    }
}
